<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Kesillo\Printing;

class Intermec
{
    private $_printerName = NULL;
    private $_printHost = NULL;
    private $_printStr = NULL;
        
    public function factory($ptr = NULL, $host = NULL) {
        return new Intermec($ptr, $host);
    }
    
    /**
     * Construcctor will assume it is a local printer unless the $host is not null
     * $ptr and $host are used to set the class' $_printStr which will be used to
     * open the connection to the pinter. If the $host is pased, we will connect
     * to a remote printer.
     * 
     * @param type $ptr name of the printer
     * @param type $host hostname for printserver
     */
    private function __construct($ptr = NULL, $host = NULL) {
        if($ptr !== NULL) {
            $this->_printerName = $ptr;
            
            if($host !== NULL) {
                $this->_printHost = $host;
                // build the printer connection string.
                $this->_printStr = "\\\\$this->_printHost\\$this->_printerName";;
            }
            else {
                $this->_printStr = $this->_printerName;
            }
        }
    }
    
    /**
     * Just makes the printer connection, sets the mode and send the commands
     * to the printer and closes the connection when done.
     * 
     * @param type $BarCode
     * @return string if error occurs when attempting to connect printer
     */
    private function IsimPrint($BarCode = NULL)
    {
        $descriptorspec = array(
            0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
            1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
            2 => array("file", "/tmp/error-output.txt", "a")
        );
        
        $command = "lp -h 192.168.50.201 -d $this->_printerName";
                
        $process = proc_open($command, $descriptorspec, $pipes);

        if (is_resource($process)) {
            // $pipes now looks like this:
            // 0 => writeable handle connected to child stdin
            // 1 => readable handle connected to child stdout
            // Any error output will be appended to /tmp/error-output.txt

            fwrite($pipes[0], $BarCode);
            fclose($pipes[0]);
        }
        else
            print_r ("error in process");
    }
    
    /**
     * Method is configured specifically for a particular type of labels.
     * Positioning of the barcodes and text is fixed, all we need is the data to
     * substitude. This will not make a custom label.
     * 
     * @param type $CustPN
     * @param type $SerialN
     * @param type $Rev
     * @return type $data string containing the ISim Intermec code
     */
    public function Code39TpltShipA($CustPN = NULL, $SerialN = NULL, $Rev = NULL)
    {
        $ch = chr(13); // CR character
        
        $data =  "".    $ch;
        $data .= "N".   $ch;
        $data .= "B790,190,2,3,2,6,48,N,\"$CustPN\"". $ch;
        $data .= "A770,140,2,4,1,1,N,\"CUST PN:$CustPN\"". $ch;
        $data .= "B790,90,2,3,2,6,48,N,\"$SerialN\"". $ch;
        $data .= "A770,40,2,4,1,1,N,\"SN:$SerialN\"". $ch;
        $data .= "B200,190,2,3,2,6,48,N,\"$Rev\"". $ch;
        $data .= "A200,140,2,4,1,1,N,\"REV:$Rev\"". $ch;
        $data .= "P1". $ch;
        
        $this->IsimPrint($data);
    }
    
    public function singleShipLabel($label = NULL, $labelParams = NULL)
    {
        $ch = chr(13); // CR character
        
        $data = "N".   $ch;
        $data .= "LO600,10,2,180". $ch;
        $data .= "A580,190,2,4,1,1,N,\"P/N:$labelParams[partno]\"". $ch;
        $data .= "A580,150,2,4,1,1,N,\"Serial#:$labelParams[seriallot]\"". $ch;
        $data .= "A580,90,2,2,1,1,N,\"Vendor:$labelParams[vendorid]\"". $ch;
        $data .= "A580,60,2,2,1,1,N,\"PO:$labelParams[pono]\"". $ch;
        $data .= "A170,190,2,4,1,1,N,\"Rev:$labelParams[partrev]\"". $ch;
        $data .= "A220,90,2,2,1,1,N,\"QTY:$labelParams[partqty]\"". $ch;
        $data .= "A220,60,2,2,1,1,N,\"Line Item:$labelParams[lineitem]\"". $ch;
        $data .= "b640,40,D,5,\"$label\"". $ch;
        $data .= "P1". $ch;
        
        $this->IsimPrint($data);
    }
}